package kz.asoft.am;


import java.sql.PreparedStatement;

import java.sql.SQLException;

import kz.asoft.am.common.RisksAppModule;

import oracle.adf.share.ADFContext;

import oracle.jbo.JboException;
import oracle.jbo.Session;
import oracle.jbo.ViewObject;
import oracle.jbo.server.ApplicationModuleImpl;
import oracle.jbo.server.ViewLinkImpl;
import oracle.jbo.server.ViewObjectImpl;
 import org.apache.log4j.Logger;

// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Sat Oct 05 15:23:31 UZT 2013
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class RisksAppModuleImpl extends ApplicationModuleImpl implements RisksAppModule {
     private static Logger LOGGER = Logger.getLogger(RisksAppModuleImpl.class.getName());

    /**
     * This is the default constructor (do not remove).
     */
    public RisksAppModuleImpl() {
    }

    /**
     * Container's getter for SCtrlObjGroupsView1.
     * @return SCtrlObjGroupsView1
     */
    public ViewObjectImpl getSCtrlObjGroupsView1() {
        return (ViewObjectImpl)findViewObject("SCtrlObjGroupsView1");
    }

    /**
     * Container's getter for SUnitsView1.
     * @return SUnitsView1
     */
    public ViewObjectImpl getSUnitsView1() {
        return (ViewObjectImpl)findViewObject("SUnitsView1");
    }

    /**
     * Container's getter for RDataValue1Link1.
     * @return RDataValue1Link1
     */
    public ViewLinkImpl getRDataValue1Link1() {
        return (ViewLinkImpl)findViewLink("RDataValue1Link1");
    }

    /**
     * Container's getter for RDataValue2Link1.
     * @return RDataValue2Link1
     */
    public ViewLinkImpl getRDataValue2Link1() {
        return (ViewLinkImpl)findViewLink("RDataValue2Link1");
    }

    /**
     * Container's getter for RDataTypeLink1.
     * @return RDataTypeLink1
     */
    public ViewLinkImpl getRDataTypeLink1() {
        return (ViewLinkImpl)findViewLink("RDataTypeLink1");
    }

    /**
     * Container's getter for SUnitsLink1.
     * @return SUnitsLink1
     */
    public ViewLinkImpl getSUnitsLink1() {
        return (ViewLinkImpl)findViewLink("SUnitsLink1");
    }

    /**
     * Анализ периодов охвата субъектов квазигосударственного сектора контрольными мероприятиями СК
     * @param yearFrom
     * @param yearTo
     */
    public void getCoverCtrlObjReport(Integer yearFrom, Integer yearTo) {
        ViewObject coverCtrlObjVO = getGCoverCtrlObjLink1().getDestination();
        coverCtrlObjVO.setWhereClause(" (YEAR BETWEEN " + yearFrom + " AND " +
                                      yearTo + ") ");
        coverCtrlObjVO.executeQuery();
    }

    protected void prepareSession(Session session) {
        super.prepareSession(session);
        // do session-specific initializations
        this.callStroredProcedure();
    }

    protected void callStroredProcedure() {
        //   LOGGER.debug("START");
        PreparedStatement st = null;
        int updatedRow = 0;
//        try {
//            String stmt =
//                "begin pkg_audit.setUserName('" + this.getUserName() +
//                "'); end;";
//            st = getDBTransaction().createPreparedStatement(stmt, 0);
//            updatedRow = st.executeUpdate();
//        } catch (SQLException e) {
//            LOGGER.error(e);
//            throw new JboException(e);
//        } catch (Exception e) {
//            LOGGER.error(e);
//        } finally {
//            if (st != null) {
//                try {
//                    st.close();
//                } catch (SQLException e) {
//                    LOGGER.error(e);
//                    e.printStackTrace();
//                }
//            }
//        }
            LOGGER.debug("FINISH");
    }

    public static String getUserName() {
            LOGGER.debug("START");
        ADFContext adfContext = ADFContext.getCurrent();
        String userName = adfContext.getSecurityContext().getUserName();
            LOGGER.debug("userName - " + userName);
        return userName;
        //        LOGGER.debug("FINISH");
    }

    /**
     * Container's getter for GCoverCtrlObjView1.
     * @return GCoverCtrlObjView1
     */
    public ViewObjectImpl getGCoverCtrlObjView1() {
        return (ViewObjectImpl)findViewObject("GCoverCtrlObjView1");
    }

    /**
     * Container's getter for GCoverCtrlObjLink1.
     * @return GCoverCtrlObjLink1
     */
    public ViewLinkImpl getGCoverCtrlObjLink1() {
        return (ViewLinkImpl)findViewLink("GCoverCtrlObjLink1");
    }
}
